﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Ops.Mpc.ExampleApplication
{
    public partial class BusyControl : UserControl
    {
        public static DependencyProperty HideProperty = DependencyProperty.Register("Hide", typeof(bool), typeof(BusyControl), new PropertyMetadata(new PropertyChangedCallback(OnHideChanged)));
        public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(BusyControl), new PropertyMetadata(new PropertyChangedCallback(OnTextChanged)));


        public bool Hide
        {
            get
            {
                return (bool)this.GetValue(HideProperty);
            }

            set
            {
                this.SetValue(HideProperty, value);
            }
        }

        public string Text
        {
            get
            {
                return (string)this.GetValue(TextProperty);
            }

            set
            {
                this.SetValue(TextProperty,value);
            }
        }

        public BusyControl()
        {
            InitializeComponent();
            this.Hide = true;
        }

        private static void OnHideChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            BusyControl busyControl = dependencyObject as BusyControl;
            busyControl.Visibility = ((bool)dependencyPropertyChangedEventArgs.NewValue) ? Visibility.Collapsed : Visibility.Visible;

            if ((bool)dependencyPropertyChangedEventArgs.NewValue)
                busyControl.Animation.Stop();
            else
                busyControl.Animation.Begin();

        }

        private static void OnTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            BusyControl busyControl = dependencyObject as BusyControl;
            busyControl.TextBox.Text = dependencyPropertyChangedEventArgs.NewValue as string;

        }

    }
}
